<?php
set_time_limit(0);
ini_set("memory_limit", -1);

$s = microtime(true);
echo '开发发送邮件,' . date("Y-m-d H:i:s") . "\n";

//定义应用程序路径
define ('APP', dirname(dirname(__FILE__)));

require_once APP . '/frame/Route.php';    //路由模块
require_once FRAME_ROOT_PATH . '/db/Mysql.php';               //数据库
require_once FRAME_ROOT_PATH . '/cache/FileCache.php';        //一个用文件缓存模块

//配置文件 
require_once APP . '/application/configs/application.php';

// 库文件
require_once APP . '/application/models/Template.php';
require_once APP . '/application/models/mail/class.phpmailer.php';

Mysql::setInstance("default", $cfg['db']['host'], $cfg['db']['user'], $cfg['db']['password'], $cfg['db']['name'], $cfg['db']['charset']);
FileCache::setInstance("default", APP . '/cache');

// 参数处理  {$mid} {$tid} {$stype} \"{$str_ids}
$mid = (int)$argv[1];
$tid = (int)$argv[2];
$stype = $argv[3];
$fullurl = $argv[4];
$sids = file_get_contents(APP . '/cache/tmp_str_mail.txt');

if ($cfg['debug']){
	echo "mid:{$mid} tid:{$tid} stype:{$stype} fullurl:{$fullurl} sids:{$sids}\n";
}

$db = Mysql::getInstance();
/**************************获取发送用户列表************************************/
$arr_mail = array();
if ($stype == 'all'){
	$arr_mail = $db->fetchAll("select email,code from email where mid=?", array($mid));
}elseif ($stype == 'select')
{
	$arr_mail = $db->fetchAll("select email,code from email where eid in (?)", array($sids));
}

/**************************获取发送用户列表************************************/

// for debug
//$arr_mail = array("rendong596@sohu.com", "rendong237@gmail.com", "rendong237@126.com", "59480732@qq.com");

if ($cfg['debug']){
	print_r($arr_mail);
}

/**************************获取发送模板设置************************************/
// 获取发送模板设置 $tpl => array('name' =>, 'content' =>)
$obj_tpl = new Template($db);
$tpl = $obj_tpl->show($tid);

if ($cfg['debug']){
	echo 'subject:' . $tpl['name'] .' content:' . $tpl['content'];
}

/**************************获取发送模板设置************************************/
$mail = new PHPMailer();

if ($cfg['debug'])
	$mail->SMTPDebug = 2;

$mail->SMTPKeepAlive = true;
$mail->IsSMTP();					// 启用SMTP
$mail->Host = $cfg['mail']['smtp'];			//SMTP服务器
$mail->SMTPAuth = $cfg['mail']['auth'];					//开启SMTP认证
$mail->Username = $cfg['mail']['user'];			// SMTP用户名
$mail->Password = $cfg['mail']['password'];				// SMTP密码
$mail->From = $cfg['mail']['from'];			//发件人地址
$mail->FromName = $cfg['mail']['fromname'];				//发件人

$mail->WordWrap = 80;					//设置每行字符长度

$mail->CharSet = "utf-8";				// 这里指定字符集！
$mail->Encoding = "base64"; 
$mail->IsHTML(true);					// 是否HTML格式邮件

$mail->Subject = $tpl['name'];			//邮件主题
//$mail->Body    = $tpl['content'];		//邮件内容
//**
if (is_array($arr_mail)){
	foreach ($arr_mail as $v){
		$email = $v['email'];
		$code = $v['code'];
		
		$cnurl = $fullurl . '/?a=reg&lang=cn&mail=' . urlencode($email) . '&code=' . $code .'&mid=' . $mid;
		$enurl = $fullurl . '/?a=reg&lang=en&mail=' . urlencode($email) . '&code=' . $code .'&mid=' . $mid;
		
		$content = str_replace("http://__cnreg___/", $cnurl, $tpl['content']);
		$content = str_replace("http://__enreg___/", $enurl, $content);
		
		$mail->Body = $content;
		
		$mail->ClearAddresses();
		$mail->AddAddress($email);
		if(!$mail->Send()){
		   echo "Message could not be sent.<$email> <p>\n";
		   echo "Mailer Error: " . $mail->ErrorInfo . "\n";
		}
	}
}
/***/

$mail->SmtpClose();

echo "邮件发送完毕 " . date("Y-m-d H:i:s") . ' ' . (microtime(true) - $s) . "\n\n";